package astudy.lucene;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;

public class Search {

	private static Log logger = LogFactory.getLog(Search.class);
	
	public static void main(String[] args) throws Exception {

		File indexDir = new File("D:\\projects\\globalsqft\\WebRoot\\WEB-INF\\search\\indexes\\property");

		IndexSearcher searcher = new IndexSearcher(FSDirectory.open(indexDir), true);  
		Query query = NumericRangeQuery.newFloatRange("space.buildingSize", 0f, 100000f, true, true);
        TopScoreDocCollector c = TopScoreDocCollector.create(100, true);  
        searcher.search(query, c);  

        logger.info(String.format("Number of Results: %d", c.getTotalHits()));
        ScoreDoc[] scoreDocs = c.topDocs(0, c.getTotalHits()).scoreDocs;  
 
        for (ScoreDoc scoreDoc : scoreDocs) {
        	Document doc = searcher.doc(scoreDoc.doc);
        	System.out.println(String.format("%8d, %4.4f: %s (%s bytes)", scoreDoc.doc, scoreDoc.score, doc.get("space.buildingSize"), doc.get("id")));
        }   
	}
}
